# Tab - Tab is group of objects that are displayed on one tab
# Copyright (C) 2007 Lauri Taimila
# 
# Entertainer is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Entertainer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

__licence__ = "GPLv2"
__copyright__ = "2008, Lauri Taimila"
__author__ = "Lauri Taimila <lauri@taimila.com>"

import gtk
import clutter
from frontend.user_event import UserEvent

class Tab(clutter.Group):
    """
    Tab can be used as part of the TabGroup
    
    Tab is a very simple container that contains all the widgets and logic
    of the tab page.
    """
    
    def __init__(self, title="Untitled tab"):
        """
        Initialize tab
        @param title: Title of the Tab (string)
        """
        clutter.Group.__init__(self)
        self.title = title
        self.active = False
        
        self.timeline = clutter.Timeline(15,60)
        self.alpha = clutter.Alpha(self.timeline, clutter.smoothstep_inc_func)
        self.behaviour = clutter.BehaviourOpacity(0, 255, self.alpha)
        self.behaviour.apply(self)
    
    def can_activate(self):
        """
        Returns a boolean whether or not this tab can be activated. If tab
        doesn't have any controlling widgets it shouldn't activate ever. 
        Default value is False. Override this method when implementing a new
        tab and add logic if needed.
        """
        return False
    
    def is_active(self):
        """
        Is this tab active
        @return: boolean
        """
        return self.active
    
    def set_active(self, boolean):
        """
        Set this tab to active or inactive state
        @param boolean: True to set active, False to set inactive
        """
        self.active = boolean

    def show_empty_tab_notice(self, title="Empty tab", message_body="This tab doesn't contain any elements."):
        """
        Create an information box that is displayed if there is no widgets in
        this tab. This method should be called only from child class as needed.
        @param title: Title text as string
        @param message_body: Message body text as string
        """
                
        # Create warning icon
        icon = gtk.gdk.pixbuf_new_from_file(self.theme.getImage("warning_icon"))
        self.info_icon = clutter.Texture()
        self.info_icon.set_pixbuf(icon)
        self.info_icon.set_position(self.ui.x(0.28),self.ui.y(0.27))
        self.info_icon.show()
        self.add(self.info_icon)
    
        # Create warning title
        self.info_title = clutter.Label()
        self.info_title.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.0625)) + "px")
        self.info_title.set_text(title)
        self.info_title.set_position(self.ui.x(0.33), self.ui.y(0.27))
        self.info_title.set_color((255,255,255,255))
        self.info_title.show()
        self.add(self.info_title)
        
        # Create warning help text
        self.info = clutter.Label()
        self.info.set_text(message_body)
        self.info.set_width(self.ui.x(0.57))
        self.info.set_position(self.ui.x(0.28), self.ui.y(0.4))
        self.info.set_line_wrap(True)
        self.info.set_font_name(self.theme.getFont() + " " + str(self.ui.y(0.04167)) + "px")
        self.info.set_color(self.theme.getColor("menuitem_inactive"))
        self.info.show()
        self.add(self.info)
    
    def set_title(self, title):
        """
        Set title of the tab. This is displayed in Tab bar
        @param title: Title string
        """
        self.title = title
    
    def get_title(self):
        """
        Get title of the tab
        @return: Title string
        """
        return self.title
    
    def show_tab(self):
        """
        Activate this tab. This is called when tab is changed and this tab
        is becoming active. This method should activate animations.
        """
        self.set_opacity(0)
        self.show()
        self.timeline.set_direction(clutter.TIMELINE_FORWARD)
        self.timeline.start()
    
    def hide_tab(self):
        """
        Deactivate this tab. This is called when tab is changed and this tab
        is becoming non-active. This method should disable animations.
        """
        self.set_opacity(0)
        self.hide()
        #self.timeline.set_direction(clutter.TIMELINE_BACKWARD)
        #self.timeline.start()
    
    def handle_user_event(self, event):
        """ 
        Handle screen specific user events
        @param event: UserEvent object
        @return: Boolean, Should control be moved back to tab bar from tab
        """
        pass